package py.com.fms.fms.persistence.services;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import com.ibatis.sqlmap.client.SqlMapClient;

import py.com.fms.exception.CommonException;
import py.com.fms.persistence.services.FmsServices;


import py.com.fms.fms.bean.PersonBean;
import py.com.fms.fms.bean.PersonManagerBean;
import py.com.fms.fms.bean.PersonAddressBean;
import py.com.fms.fms.bean.PersonDocumentBean;
import py.com.fms.fms.bean.PersonPhoneBean;

@SuppressWarnings("unused")
public class PersonServices extends FmsServices {

	public PersonServices(SqlMapClient client) {
		super(client);
		// TODO Auto-generated constructor stub
	}
	
	/**
	 [[[
	 [[ The following method retrieve a list of all records
	 [[ Normally used to load a grid.
	 */
	
	@SuppressWarnings("unchecked")
	public List<PersonBean> getAllPersons() throws CommonException{
		List<PersonBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface that receive as argument the Paises ]] 
			//[[ namespace located in package py.com.fms.persistence.sqlmap.sql.fms.fmsPaises.xml   ]]
			
			queryForList = (List<PersonBean>)this.getConfig().queryForList("person.getAllPerson");
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<PersonBean> getPersonSearch(PersonBean bean) throws CommonException{
		List<PersonBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface that receive as argument the Paises ]] 
			//[[ namespace located in package py.com.fms.persistence.sqlmap.sql.fms.fmsPaises.xml   ]]
			//System.out.println(bean.getCod() + bean.getName() + bean.getRuc() + bean.);
			queryForList = (List<PersonBean>)this.getConfig().queryForList("person.getPersonSearch", bean);
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<PersonAddressBean> getAllPersonAddress() throws CommonException{
		List<PersonAddressBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface that receive as argument the Paises ]] 
			//[[ namespace located in package py.com.fms.persistence.sqlmap.sql.fms.fmsPaises.xml   ]]
			
			queryForList = (List<PersonAddressBean>)this.getConfig().queryForList("person.getAllAddress");
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<PersonDocumentBean> getAllPersonDoc() throws CommonException{
		List<PersonDocumentBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface that receive as argument the Paises ]] 
			//[[ namespace located in package py.com.fms.persistence.sqlmap.sql.fms.fmsPaises.xml   ]]
			
			queryForList = (List<PersonDocumentBean>)this.getConfig().queryForList("person.getAllDoc");
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<PersonPhoneBean> getAllPersonPhone() throws CommonException{
		List<PersonPhoneBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface that receive as argument the Paises ]] 
			//[[ namespace located in package py.com.fms.persistence.sqlmap.sql.fms.fmsPaises.xml   ]]
			
			queryForList = (List<PersonPhoneBean>)this.getConfig().queryForList("person.getAllPhone");
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public List<PersonAddressBean> getPersonsAddress(Long idPerson) throws CommonException{
		List<PersonAddressBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface that receive as argument the Paises ]] 
			//[[ namespace located in package py.com.fms.persistence.sqlmap.sql.fms.fmsPaises.xml   ]]
			
			queryForList = (List<PersonAddressBean>)this.getConfig().queryForList("person.getPersonAddress", idPerson);
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<PersonDocumentBean> getPersonsDoc(Long idPerson) throws CommonException{
		List<PersonDocumentBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface that receive as argument the Paises ]] 
			//[[ namespace located in package py.com.fms.persistence.sqlmap.sql.fms.fmsPaises.xml   ]]
			
			queryForList = (List<PersonDocumentBean>)this.getConfig().queryForList("person.getPersonDoc", idPerson);
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<PersonPhoneBean> getPersonsPhone(Long idPerson) throws CommonException{
		List<PersonPhoneBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface that receive as argument the Paises ]] 
			//[[ namespace located in package py.com.fms.persistence.sqlmap.sql.fms.fmsPaises.xml   ]]
			
			queryForList = (List<PersonPhoneBean>)this.getConfig().queryForList("person.getPersonPhone", idPerson);
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	

	public PersonBean getPersonsData(Long idPerson) throws CommonException{
		PersonBean queryForObject;
		try {
			
			//[[ queryForList is an implemented interface that receive as argument the Paises ]] 
			//[[ namespace located in package py.com.fms.persistence.sqlmap.sql.fms.fmsPaises.xml   ]]
			
			queryForObject = (PersonBean)this.getConfig().queryForObject("person.getPersonData", idPerson);
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	/**
	 [[[
	 [[ The following methods store a bean (record) in the database.
	 */
	
	//INSERT A PERSON RECORD
	public void saveNewPerson(PersonBean person) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().insert("person.insPerson", person);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	//INSERT THE PERSON ADDRESS
	public void savePersonAddress(PersonAddressBean address) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().insert("person.insPersonAddress", address);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	//INSERT THE PERSON DOCUMENT
	public void savePersonDoc(PersonDocumentBean document) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().insert("person.insPersonDoc", document);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	//INSERT THE PERSON PHONE
	public void savePersonPhone(PersonPhoneBean phone) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().insert("person.insPersonPhone", phone);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 [[[
	 [[ The following method delete a record from the database.
	 */
	
	public void deletePersonById(long id) throws CommonException{
		try {
			HashMap<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("idPerson", id);
			this.getConfig().startTransaction();
			this.getConfig().delete("person.delPersonById", parameters);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	public void deletePersonAddrById(long id) throws CommonException{
		try {
			HashMap<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("idAddress", id);
			this.getConfig().startTransaction();
			this.getConfig().delete("person.delPersonAddrById", parameters);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	public void deletePersonDocById(long id) throws CommonException{
		try {
			HashMap<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("idDoc", id);
			this.getConfig().startTransaction();
			this.getConfig().delete("person.delPersonDocById", parameters);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	public void deletePersonPhoneById(long id) throws CommonException{
		try {
			HashMap<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("idPhone", id);
			this.getConfig().startTransaction();
			this.getConfig().delete("person.delPersonPhoneById", parameters);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 [[[
	 [[ The following methods update a record from a bean.
	 */
	public void updatePerson(PersonBean person) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("person.udpPersonById", person);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	public void updateAddress(PersonAddressBean address) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("person.udpAddressById", address);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	public void updateDoc(PersonDocumentBean document) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("person.udpDocById", document);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	public void updatePhone(PersonPhoneBean phone) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("person.udpPhoneById", phone);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 [[[
	 [[ The following methods retrieve a object containing the record id
	 */
	public Long getIdPerson(String cod) throws CommonException{
		long queryForObject;
		try {
			
			//[[ queryForList is an implemented interface that receive as argument the monedas ]] 
			//[[ namespace located in package py.com.fms.persistence.sqlmap.sql.fms.fmsMonedas.xml   ]]
			System.out.println("(Person Service)Codigo: " + cod);
			queryForObject = (Long)this.getConfig().queryForObject("person.getIdPerson", cod);
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	public String getDescPerson(Long id) throws CommonException{
		String queryForObject;
		try {
			
			//[[ queryForList is an implemented interface that receive as argument the monedas ]] 
			//[[ namespace located in package py.com.fms.persistence.sqlmap.sql.fms.fmsMonedas.xml   ]]
			System.out.println("(Service)Id: " + id);
			queryForObject = (String)this.getConfig().queryForObject("person.getDescPerson", id);
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	public String getAddressPerson(Long idPer) throws CommonException{
		String queryForObject;
		try {
			
			//[[ queryForList is an implemented interface that receive as argument the id ]] 
			System.out.println("(Service)Codigo: " + idPer);
			queryForObject = (String)this.getConfig().queryForObject("person.getAddressPerson", idPer);
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	public PersonBean getPersonData(Long id) throws CommonException{
		PersonBean queryForObject;
		try {
			
			//[[ queryForList is an implemented interface that receive as argument the Paises ]] 
			//[[ namespace located in package py.com.fms.persistence.sqlmap.sql.fms.fmsPaises.xml   ]]
			
			queryForObject = (PersonBean)this.getConfig().queryForObject("person.getPersonData");
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}

}
